using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Sellingleads : System.Web.UI.Page
{
    #region Region
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ProductSell._Table + "].[" + ProductSellColumns.CreatedDate + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }

    public string Title
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Title"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Title"];
        }
    }
    public long CatPro
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["CatPro"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["CatPro"]);
        }
    }
    public string appro
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["appro"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["appro"];
        }
    }
    public string Pen
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Pen"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Pen"];
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadCat();
        LoadData();
        LoadCurent();
        LoadItemcout();
        txtsearch.Value = this.Title;
        ddlCat.SelectedValue = this.CatPro.ToString();
    }
    #region LoadCurent
    private void LoadCurent()
    {
        ContentPlaceHolder c = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
        UserControl usc = c.FindControl("MainMenu1") as UserControl;
        HtmlAnchor hrfSellingleads = (HtmlAnchor)usc.FindControl("hrfSellingleads");
        hrfSellingleads.Attributes.Add("class", "selected1");
    }

    #endregion
    #region Load dropdownlist here

    private void LoadCat()
    {
        ddlCat.Items.Clear();
        ddlCat.Items.Add(new ListItem("- Select Category -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 2, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Enable + "]", 1, Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductCatCollection.Count > 0)
        {
            foreach (ProductCat _ProductCat in _ProductCatCollection)
            {
                ddlCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
            }
        }
    }
    #endregion Load about dropdownlist
    #region Load Itme cout
    private void LoadItemcout()
    {
        #region Load all
        Criteria cri = new Criteria();
        if (Session["MemberId"] != null)
        {
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.MemberId + "]", long.Parse(Session["MemberId"].ToString()), Condition.EQUAL, Condition.AND);
        }
        else
        {
            Response.Redirect("MemberLogin.aspx");
        }
        cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Not_Clear + "]", 0, Condition.EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, cri.Criter);
        ltrcountall.Text = _ProductSellCollection.Count.ToString();
        #endregion
        #region Load count Approved Approval Pending
        Criteria crit = new Criteria();
        if (Session["MemberId"] != null)
        {
            crit.add("[" + ProductSell._Table + "].[" + ProductSellColumns.MemberId + "]", long.Parse(Session["MemberId"].ToString()), Condition.EQUAL, Condition.AND);
        }
        else
        {
            Response.Redirect("MemberLogin.aspx");
        }
        crit.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Approved + "]", 1, Condition.EQUAL, Condition.AND);
        crit.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Not_Clear + "]", 0, Condition.EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollectiont = ProductSellService.GetProductSellListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, crit.Criter);
        ltrApproved.Text = _ProductSellCollectiont.Count.ToString();
        #endregion
        #region Load count  Approval Pending
        Criteria critr = new Criteria();
        if (Session["MemberId"] != null)
        {
            critr.add("[" + ProductSell._Table + "].[" + ProductSellColumns.MemberId + "]", long.Parse(Session["MemberId"].ToString()), Condition.EQUAL, Condition.AND);
        }
        else
        {
            Response.Redirect("MemberLogin.aspx");
        }
        critr.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Approved + "]", 0, Condition.EQUAL, Condition.AND);
        critr.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Not_Clear + "]", 0, Condition.EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollectiontr = ProductSellService.GetProductSellListByParam(ProductSellColumns.ProductId, OrderDirection.ASC, critr.Criter);
        ltrApprovalPending.Text = _ProductSellCollectiontr.Count.ToString();
        #endregion

    }
    #endregion
    #region LoadData
    private string username = string.Empty;
    private void LoadData()
    {

        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        cri.addJoin(ProductCat._Table, "[" + ProductSell._Table + "].[" + ProductSellColumns.CatId + "]", "[" + ProductCat._Table + "].[" + ProductCatColumns.ProCateID + "]", Condition.EQUAL, Condition.LEFT_JOIN);
        if (!string.IsNullOrEmpty(this.Title))
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Title + "]", this.Title, Condition.LIKE, Condition.AND);
        if (!string.IsNullOrEmpty(this.appro))
        {
            liappro.Attributes.Add("class", "");
            liall.Attributes.Add("class", "other");
            lipen.Attributes.Add("class", "other");
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Approved + "]", 1, Condition.LIKE, Condition.AND);
        }
        if (!string.IsNullOrEmpty(this.Pen))
        {
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Approved + "]", 0, Condition.LIKE, Condition.AND);
            lipen.Attributes.Add("class", "");
            liappro.Attributes.Add("class", "other");
            liall.Attributes.Add("class", "other");
        }
        if (string.IsNullOrEmpty(this.Pen) && string.IsNullOrEmpty(this.appro))
        {
            liappro.Attributes.Add("class", "other");
            liall.Attributes.Add("class", "");
            lipen.Attributes.Add("class", "other");
        }
        if (this.CatPro != long.MinValue && this.CatPro != 0)
        {
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.CatId + "]", this.CatPro, Condition.EQUAL, Condition.AND);
        }
        cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.Not_Clear + "]", 0, Condition.LIKE, Condition.AND);
        if (Session["MemberId"] != null)
        {
            cri.add("[" + ProductSell._Table + "].[" + ProductSellColumns.MemberId + "]", long.Parse(Session["MemberId"].ToString()), Condition.EQUAL, Condition.AND);
        }
        else
        {
            Response.Redirect("MemberLogin.aspx");
        }
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(hidColSort, (OrderDirection)Session["Order"], hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);

        rptList.DataSource = _ProductSellCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;

    }

    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ProductSell _ProductSell = (ProductSell)e.Item.DataItem;
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HtmlTableRow tr = (HtmlTableRow)e.Item.FindControl("tr");
            Literal ltrCat = (Literal)e.Item.FindControl("ltrCat");
            CheckBox ckEnable = (CheckBox)e.Item.FindControl("ckEnable");
            Literal ltrUpdatedate = (Literal)e.Item.FindControl("ltrUpdatedate");
            Literal ltrTitle = (Literal)e.Item.FindControl("ltrTitle");

            Literal ltrimgeTitle = (Literal)e.Item.FindControl("ltrimgeTitle");
            Literal ltrimgDateUpdate = (Literal)e.Item.FindControl("ltrimgDateUpdate");
            string strorder = this.hidColSort;

            HtmlImage img = (HtmlImage)e.Item.FindControl("img");
            if (!string.IsNullOrEmpty(_ProductSell.Image))
            {
                img.Src = "photos/" + _ProductSell.Image;
            }
            else
            {
                img.Visible = false;
            }

            ltrTitle.Text = _ProductSell.Title;
            ProductCat _ProductCatSubSub = ProductCatService.GetProductCat(_ProductSell.CatId);
            if (_ProductCatSubSub != null)
            {
                ltrCat.Text = _ProductCatSubSub.Name;
            }
            ltrUpdatedate.Text = Utility.GetFormattedDate(_ProductSell.UpdatedDate);

            hidID.Value = _ProductSell.ProductId.ToString();
            int STT = e.Item.ItemIndex + 1;
            if (STT % 2 != 0)
            {
                tr.Attributes["class"] = "bg_gray";
            }
            else
            {
                tr.Attributes["class"] = "none";
            }
        }
        #region Load about header
        if (e.Item.ItemType == ListItemType.Header)
        {
            Literal ltrimgTitleUp = (Literal)e.Item.FindControl("ltrimgTitleUp");
            Literal ltrimgTitleUpDow = (Literal)e.Item.FindControl("ltrimgTitleUpDow");
            if (hidColSort == "[ProductSell].[Title]")
            {
                if ((OrderDirection)Session["Order"] == OrderDirection.DESC)
                {
                    ltrimgTitleUp.Text = "<img src='images/up.gif' width='5' height='3' border='0' />";
                    ltrimgTitleUpDow.Text = "<img src='images/down1.gif' width='5' height='3' border='0' style='margin-top: 2px;' />";

                }
                else if ((OrderDirection)Session["Order"] == OrderDirection.ASC)
                {
                    ltrimgTitleUp.Text = "<img src='images/up1.gif' width='5' height='3' border='0' />";
                    ltrimgTitleUpDow.Text = "<img src='images/down.gif' width='5' height='3' border='0' style='margin-top: 2px;' />";
                }

            }
            else
            {
                ltrimgTitleUp.Text = "<img src='images/up.gif' width='5' height='3' border='0' />";
                ltrimgTitleUpDow.Text = "<img src='images/down.gif' width='5' height='3' border='0'style='margin-top: 2px;' />";
            }
            Literal ltrimgTitleUpDate = (Literal)e.Item.FindControl("ltrimgTitleUpDate");
            Literal ltrimgTitleUpDowDate = (Literal)e.Item.FindControl("ltrimgTitleUpDowDate");
            if (hidColSort == "[ProductSell].[UpdatedDate]")
            {
                if ((OrderDirection)Session["Order"] == OrderDirection.DESC)
                {
                    ltrimgTitleUpDate.Text = "<img src='images/up.gif' width='5' height='3' border='0' />";
                    ltrimgTitleUpDowDate.Text = "<img src='images/down1.gif' width='5' height='3' border='0' style='margin-top: 2px;' />";

                }
                else if ((OrderDirection)Session["Order"] == OrderDirection.ASC)
                {
                    ltrimgTitleUpDate.Text = "<img src='images/up1.gif' width='5' height='3' border='0' />";
                    ltrimgTitleUpDowDate.Text = "<img src='images/down.gif' width='5' height='3' border='0' style='margin-top: 2px;' />";
                }

            }
            else
            {
                ltrimgTitleUpDate.Text = "<img src='images/up.gif' width='5' height='3' border='0' />";
                ltrimgTitleUpDowDate.Text = "<img src='images/down.gif' width='5' height='3' border='0'style='margin-top: 2px;' />";
            }
        }
        #endregion

    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
        string commandName = e.CommandName;
        object obj = new object();
        switch (commandName)
        {
            case "sortTitle":
                obj = "[" + ProductSell._Table + "].[" + ProductSellColumns.Title + "]";
                break;
            case "sortUpdatedate":
                obj = "[" + ProductSell._Table + "].[" + ProductSellColumns.UpdatedDate + "]";
                break;
            default:
                break;
        }
        if ((OrderDirection)Session["Order"] == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        LoadData();
    }
    #endregion
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HtmlInputCheckBox chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                ProductSell _ProductSell = ProductSellService.GetProductSell(ID);
                _ProductSell.Not_Clear = true;
                ProductSellService.UpdateProductSell(_ProductSell);
            }
        }
        LoadData();
        LoadItemcout();
    }
    protected void lndeletes_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HtmlInputCheckBox chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                ProductSell _ProductSell = ProductSellService.GetProductSell(ID);
                _ProductSell.Not_Clear = true;
                ProductSellService.UpdateProductSell(_ProductSell);
            }
        }
        LoadData();
        LoadItemcout();
    }
    #endregion
    #region Event button

    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("Sellingleads.aspx?Title={0}&CatPro={1}", txtsearch.Value.Trim(), ddlCat.SelectedValue));
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("Sellingleads.aspx");
    }
    #endregion

    // private Repeater docList;
    //docList = new Repeater();
    //docList.DataSource = documentTypes;
    //docList.DataBind();
    //foreach (RepeaterItem repeatItem in docList.Items) { 
    //// if condition to add HeaderTemplate Dynamically only Once if (repeatItem.ItemIndex == 0) 
    //{ RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
    //HtmlGenericControl hTag = new HtmlGenericControl("h4"); 
    //hTag.InnerHtml = "Header";
    //repeatItem.Controls.Add(hTag); } // Add ItemTemplate DataItems Dynamically 
    //RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
    //Label lbl = new Label(); // This part is completely broken! 
    //lbl.Text = string.Format("Content: {0} {1} <br />", (DocumentType)repeaterItem.DataItem).ID, repeaterItem.NamingContainer); repeatItem.Controls.Add(lbl); // Add SeparatorTemplate Dynamically repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator); LiteralControl ltrlHR = new LiteralControl(); ltrlHR.Text = "<hr />"; repeatItem.Controls.Add(ltrlHR); }
}
